<?php

namespace App\Http\Controllers\admin\api;

use App\Http\Controllers\admin\validate\Equipment;
use App\Http\Controllers\utils\ToolUtil;
use Illuminate\Http\Request;
use App\Http\Controllers\utils\LogUtil;
use Illuminate\Support\Facades\DB;

class EquipmentApi
{


    /**
     * 获取sn信息
     * @param $param
     * @return false|string
     */
    public static function getSnInfoById(Request $request){

        $param = $request->all();
        if(empty($param['id'])){
            return ToolUtil::returnJson(401,'编号为空');
        }

        $id = (int)$param['id'];
        $t_admin_sn = DB::table('t_admin_sn as s')->where('s.id',$id)->first(['s.id','s.type','s.type_id']);
        if(!$t_admin_sn){
            return ToolUtil::returnJson(400,'没有数据');
        }

        return ToolUtil::returnJson(200,'成功',$t_admin_sn);

    }
    
    /**
     * 批量编辑sn类型
     * @param $param
     * @return false|string
     */
    public static function multipleUpdateSnType(Request $request){

        $param = $request->all();
        $param_msg = Equipment::multipleUpdateSnType($param);
        if ($param_msg) {
            return ToolUtil::returnJson(401, $param_msg[0]);
        }

        $userId = (int)$param['userId'];
        if(!ToolUtil::isSuperAdmin($userId)){
            return ToolUtil::returnJson(301,'没有权限');
        }

        $type = (int)$param['type'];
        $type_id = (int)$param['type_id'];

        try {

            if(!ToolUtil::checkExcelSuffix($param)){
                return ToolUtil::returnJson(302,'文件类型必须为xlsx或xls');
            }
            $phpReader = new \PHPExcel_Reader_Excel2007();
            $phpExcel = $phpReader->load($param['file']);
            $currentSheet = $phpExcel->getSheet(0);//获取第一个表单
            $allRow = $currentSheet->getHighestRow();//获取总行数
            $maxColumn = $currentSheet->getHighestColumn();//获取最大列号
            $allColumn =\PHPExcel_Cell::columnIndexFromString($maxColumn);//获取总列数

            if($allRow > 1001){
                return ToolUtil::returnJson(302,'不能超过1000条');
            }

            $snLenArr = [];//记录不等于长度的sn码行数
            $success = 0;//记录成功数
            $errorAll = [];//记录失败行数
            $createTime = time();
            $param['remark'] = '批量编辑sn类型';
            DB::beginTransaction();
            set_time_limit(0);
            $push_sn = [];
            for ($currentRow=2;$currentRow<=$allRow;$currentRow++){
                for ($currentColumn=0;$currentColumn<$allColumn;$currentColumn++){
                    $val = trim($currentSheet->getCellByColumnAndRow($currentColumn,$currentRow)->getValue());
                    if(strlen($val) < 2){
                        $snLenArr[$currentRow] = $currentRow;
                        break;
                    }

                    $res = DB::table('t_admin_sn')->where('value',$val)->update(['type'=>$type,'type_id'=>$type_id,'update_time'=>time()]);
                    if($res){
                        $push_sn[] = $val;
                        $success++;
                        $param['details'] = ToolUtil::handleSuccessOrError(['sn'=>$val]);
                        LogUtil::addAdminLog($param);
                        break;
                    }else{
                        $errorAll[$currentRow] = $currentRow;
                    }

                }
            }
            DB::commit();

            $push_param['type'] = 2;
            $push_param['statusType'] = '编辑sn类型';
            $push_param['title'] = 'OK';
            $push_param['batch'] = $push_sn;
            $push_param['status'] = 1;
            $push_param['contentType'] = 17;
            $push_param['userId'] = (int)$param['userId'];
            $jPush = LogUtil::addPushLog($push_param);

            $result['success'] = $success;
            $result['total'] = $allRow-1;
            $result['snLen'] = array_values($snLenArr);
            $result['errorRow'] = array_values($errorAll);
            return ToolUtil::returnJson(200,'成功',$result);
        }catch (\Exception $exception){
            return ToolUtil::returnJson(500,'格式错误');
        }

    }   

    
    /**
     * 编辑sn类型
     * @param $param
     * @return false|string
     */
    public static function updateSnType(Request $request){

        $param = $request->all();
        $param_msg = Equipment::updateSnType($param);
        if ($param_msg) {
            return ToolUtil::returnJson(401, $param_msg[0]);
        }

        $userId = (int)$param['userId'];
        $sn_id = (int)$param['sn_id'];
        $where = ['id'=>$sn_id];
        if(!ToolUtil::isSuperAdmin($userId)){
            return ToolUtil::returnJson(301,'没有权限');
        }
        $sn_rec = DB::table('t_admin_sn')->where($where)->first(['value']);
        if(empty($sn_rec)){
            return ToolUtil::returnJson(301,'不存在该设备');
        }
        
        $type = (int)$param['type'];
        $type_id = (int)$param['type_id'];

        $res = DB::table('t_admin_sn')->where('id',$sn_id)->update(['type'=>$type,'type_id'=>$type_id,'update_time'=>time()]);
        if($res){

            $push_param['type'] = 2;
            $push_param['statusType'] = '编辑sn类型';
            $push_param['title'] = 'OK';
            $push_param['batch'] = array($sn_rec['value']);
            $push_param['status'] = 1;
            $push_param['contentType'] = 17;
            $push_param['userId'] = (int)$param['userId'];
            $jPush = LogUtil::addPushLog($push_param);
            return ToolUtil::returnJson(200,'成功.');

        }
        return ToolUtil::returnJson(401,'失败');

    }
    


}